package com.yjt.zdjf.standard.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent;
import org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent;
import org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent;
import org.springframework.integration.ip.tcp.serializer.TcpDeserializationExceptionEvent;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

@Service(ZDJFService.PACKAGE_CLASS_NAME)
public class ZDJFService {

	private static Logger logger = LoggerFactory.getLogger(ZDJFService.class);
	
	public static final String PACKAGE_CLASS_NAME = "com.yjt.zdjf.standard.service.ZDJFService";
	
	public void processError(Message<?> message) throws Exception {
		if (message.getPayload() instanceof TcpConnectionExceptionEvent) {
			TcpConnectionExceptionEvent tcpConnectionExceptionEvent = (TcpConnectionExceptionEvent) message.getPayload();
			logger.error("[主动缴费服务层]捕获到TcpConnectionException：ConnectionId为"+tcpConnectionExceptionEvent.getConnectionId()+"的请求在报文处理时抛出ConnectionException，具体信息如下："+message, tcpConnectionExceptionEvent.getCause());
		} else if (message.getPayload() instanceof TcpDeserializationExceptionEvent) {
			TcpDeserializationExceptionEvent tcpDeserializationExceptionEvent = (TcpDeserializationExceptionEvent) message.getPayload();
			logger.error("[主动缴费服务层]捕获到TcpDeserializationException："+tcpDeserializationExceptionEvent.getSource()+"在请求报文处理时抛出DeserializationException，具体信息如下："+message, tcpDeserializationExceptionEvent.getCause());
		} else if(message.getPayload() instanceof MessagingException) {
			MessagingException exceptionPayload = (MessagingException) message.getPayload();
			logger.error("[主动缴费服务层]捕获到MessagingException："+ message, exceptionPayload.getCause());
		} else{
			logger.error("[主动缴费服务层]捕获到未知类型异常："+message);
		}
	}
	
	public void processTCPConnectionEvents(Message<?> message) {
		if(message.getPayload() instanceof TcpConnectionOpenEvent){
			TcpConnectionOpenEvent tcpConnectionOpenEvent=(TcpConnectionOpenEvent)message.getPayload();
			logger.info("[主动缴费服务层]监听到TcpConnectionOpenEvent："+ tcpConnectionOpenEvent.toString());
		}else if (message.getPayload() instanceof TcpConnectionCloseEvent){
			TcpConnectionCloseEvent tcpConnectionCloseEvent=(TcpConnectionCloseEvent)message.getPayload();
			logger.info("[主动缴费服务层]监听到TcpConnectionCloseEvent："+ tcpConnectionCloseEvent.toString());
		}else if (message.getPayload() instanceof TcpConnectionExceptionEvent) {
			TcpConnectionExceptionEvent tcpConnectionExceptionEvent = (TcpConnectionExceptionEvent) message.getPayload();
			logger.error("[主动缴费服务层]监听到TcpConnectionExceptionEvent："+message, tcpConnectionExceptionEvent.getCause());
		} else if (message.getPayload() instanceof TcpDeserializationExceptionEvent) {
			TcpDeserializationExceptionEvent tcpDeserializationExceptionEvent = (TcpDeserializationExceptionEvent) message.getPayload();
			logger.error("[主动缴费服务层]监听到TcpDeserializationExceptionEvent："
					+"原因为"+tcpDeserializationExceptionEvent.getCause()
					+"，buffer中产生异常的offser为"+tcpDeserializationExceptionEvent.getOffset()
					+"，具体信息如下："+message, tcpDeserializationExceptionEvent.getCause());
		}else{
			logger.error("[主动缴费服务层]监听到一个未知类型的IpIntegrationEvent："+message);
		}
	}
}
